﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MVCPets.Extensions;
using System.Collections.Specialized;

namespace MVCPets.Tests.Extensions
{
  [TestClass]
  public class DictionaryExtensions
  {
    private Dictionary<string, string> _ssDictionary;
    private Dictionary<string, object> _soDictionary;
    private NameValueCollection _nvC;

    [TestInitialize]
    public void Init()
    {
      ConfigureDictionaries();
    }

    public void ConfigureDictionaries()
    {
      _ssDictionary = new Dictionary<string, string>();
      _soDictionary = new Dictionary<string, object>();
      _nvC = new NameValueCollection();

      foreach (var i in Enumerable.Range(0, 100))
      {
        _ssDictionary.Add(i.ToString(), string.Format("Item_{0}", i));
        _soDictionary.Add(i.ToString(), Guid.NewGuid());
        _nvC.Add(i.ToString(), string.Format("Item_{0}", i));
      }
    }

    [TestMethod]
    public void Can_Get_String_From_String_String()
    {
      var x = _ssDictionary.GetItem<string>("1", string.Empty);

      Assert.IsNotNull(x);
      Assert.AreEqual("Item_1", x);

      var y = _ssDictionary.GetItem<string>("y", string.Empty);

      Assert.IsNotNull(y);
      Assert.AreEqual(string.Empty, y);

      var z = _ssDictionary.GetItem<string>("z", null);

      Assert.IsNull(z);
    }

    [TestMethod]
    public void Can_Get_String_From_NVC()
    {
      var x = _nvC.GetItem<string>("1", string.Empty);

      Assert.IsNotNull(x);
      Assert.AreEqual("Item_1", x);

      var y = _nvC.GetItem<string>("y", string.Empty);

      Assert.IsNotNull(y);
      Assert.AreEqual(string.Empty, y);

      var z = _nvC.GetItem<string>("z", null);

      Assert.IsNull(z);
    }

    [TestMethod]
    public void Can_Get_GUID_From_String_Object()
    {
      var x = _soDictionary.GetItem<Guid>("1", Guid.NewGuid());

      Assert.IsNotNull(x);
      Assert.IsTrue(x.ToString().Length > 0);

      var y = _soDictionary.GetItem<Guid>("y", Guid.NewGuid());

      Assert.IsNotNull(y);
      Assert.IsTrue(x != y);

      var z = _soDictionary.GetItem<object>("z", null);

      Assert.IsNull(z);
    }

    [TestMethod]
    public void Can_Parse_Double()
    {
      var foo = new Dictionary<string, string>();

      foo.Add("item1", "25.2");
      foo.Add("item2", "51.35");
      foo.Add("item3", "3.14");

      Assert.IsNotNull(foo.ParseDouble("item3"));
      Assert.IsNull(foo.ParseDouble("bar"));
    }
  }
}
